package com.example.springaop_1.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date:2022-11-01
 * Time:22:58
 *
 * @Author:东莞呵呵
 */
@Aspect
@Component
public class UserAspect {
    @Pointcut("execution(* com.example.springaop_1.controller.UserController.*(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void doBefore() {
        System.out.println("before");
    }

    @After("pointCut()")
    public void doAfter() {
        System.out.println("after");
    }

    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("AfterReturning");
    }
    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("AfterThrowing");
    }

//    @Around("pointCut()")
//    public Object doAround(ProceedingJoinPoint joinPoint){
//        Object obj=null;
//        System.out.println("Around 方法开始执行");
//        try{
//            obj=joinPoint.proceed();
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//        System.out.println("Around 方法结束执行");
//        return obj;
//    }
    @Around("pointCut()")
    public Object time(ProceedingJoinPoint joinPoint){
        Object obj=null;
        StopWatch stopWatch=new StopWatch();
        System.out.println("Around 方法开始执行");
        try{
            stopWatch.start();
            obj=joinPoint.proceed();
            stopWatch.stop();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println(joinPoint.getSignature().getName()+"方法的执行时间是:"+stopWatch.getLastTaskTimeMillis());
        System.out.println("Around 方法结束执行");
        return obj;
    }
}
